package tgc.edu.exam.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.exam.custom.AjaxResult;
import tgc.edu.exam.entity.BookRecord;
import tgc.edu.exam.entity.Books;
import tgc.edu.exam.entity.SysRole;
import tgc.edu.exam.entity.SysUser;
import tgc.edu.exam.entity.User;
import tgc.edu.exam.form.UserForm;
import tgc.edu.exam.service.BookRecordService;
import tgc.edu.exam.service.BookService;
import tgc.edu.exam.service.SysRoleService;
import tgc.edu.exam.service.SysUserService;
import tgc.edu.exam.service.UserService;

@Controller
@RequestMapping("/android")
public class AndroidController {

	@Autowired
	private BookService bookService;
	@Autowired
	private SysUserService userService;
	@Autowired
	private BookRecordService recordService;
	@Autowired
	private UserService userServ;
	@Autowired
	private SysRoleService roleService;
	
	/**
	 * Android登录请求
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping("/login")
	@ResponseBody
	public Object login(String username,String password) {
		SysUser getUser = userService.findByUsername(username);
		if("admin".equals(username)||"root".equals(username)) {
			return new AjaxResult(false, "只限用户登录");
		}
		if(getUser!=null) {
			BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
			boolean b = encoder.matches(password, getUser.getPassword());
			if(b) {
				return new AjaxResult("登录成功");
			}else {
				return new AjaxResult(false, "密码错误!");
			}
		}else{
			return new AjaxResult(false, "用户不存在");
		}
	}
	/**
	 * 调取所有书籍
	 * @return
	 */
	@RequestMapping("/bookAll")
	@ResponseBody
	public Object bookAll() {
		List<Books> list = bookService.findAll();
		return list;
	}
	/**
	 * 借书
	 * @param byid
	 * @param username
	 * @return
	 */
	@RequestMapping("/borrow")
	@ResponseBody
	public Object borrow(String byid,String username) {
		SysUser user = userService.findByUsername(username);
		BookRecord record = new BookRecord();
		record.setUsername(user.getUsername());
		record.setName(user.getName());
		User user2 = userServ.findById(user.getId());
		record.setPhone(user2.getPhone());
		Books books = bookService.findByByid(byid);
		Integer number = books.getNumber()-1;
		books.setNumber(number);
		bookService.save(books);
		record.setTitle(books.getTitle());
		record.setByid(books.getByid());
		record.setSuthor(books.getSuthor());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = df.format(new Date());
		record.setBorrowData(date);
		Date dtime = new Date();
		Date dt = stepDay(dtime , 60);
		record.setReturnData(df.format(dt));
		record.setState("等待管理员审核");
		recordService.save(record);
		return "请等待管理员审核！";
		
	}
	
	private Date stepDay(Date dtime, int day) {
		Calendar instance = Calendar.getInstance();
		instance.setTime(dtime);
		//获取天数结束的日期
		instance.add(Calendar.DATE, day);
		return instance.getTime();
	}
	/**
	 * 借书记录
	 * @param username
	 * @return
	 */
	@RequestMapping("/record")
	@ResponseBody
	public Object record(String username) {
		List<BookRecord> list = recordService.findByUsernameOrderByIdDesc(username);
		return list;
		
	}
	/**
	 * 还书
	 * @param id
	 * @return
	 */
	@RequestMapping("/returnbook")
	@ResponseBody
	public String returnbook(String byid) {
		Integer id = Integer.valueOf(byid);
		BookRecord record = recordService.findById(id);
		Books book = bookService.findByByid(record.getByid());
		Integer number = book.getNumber()+1;
		book.setNumber(number);
		bookService.save(book);
		record.setState("已还");
		recordService.save(record);
		return "还书成功";
		
	}
	/**
	 * Android端获取个人信息
	 * @param username
	 * @return
	 */
	@RequestMapping("/info")
	@ResponseBody
	public Object info(String username) {
		User model = userServ.findByUsername(username);
		return model;
	}
	/**
	 * 用户注册
	 * @param form
	 * @return
	 */
	@RequestMapping("/saveuser")
	@ResponseBody
	public String saveuser(UserForm form) {
		System.out.println(form.getUsername()+"..."+form.getName()+"..."+form.getPhone()+"..."+form.getSex()+"..."+form.getAge());
		User model = new User();
		SysRole role = roleService.findByCode("ROLE_USER");
		model.getRole().add(role);
		BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
		BeanUtils.copyProperties(form, model);
		model.setPassword(encoder.encode("123456"));
		userServ.save(model);
		return "注册成功！";
	}
}
